$(document).ready(function() {
	
	
	$('a.plus').click(function () {
		
		var galleryClass = $('#gallery div:first').attr('class');
		var zoom = Number(galleryClass.substring(4));	
		var maxzoom = 6;
		
		if ($(window).width() < 1180 && $(window).width() > 468) {
			maxzoom = 5;
		} else if ($(window).width() < 468 && $(window).width() > 390) {
			maxzoom = 4;
		} else if ($(window).width() < 390) {
			maxzoom = 4;
		} 
		
		if (zoom < maxzoom) {
			
			$("#gallery div").attr('class', 'gall' + (zoom + 1));
			
			 $.ajax({
				type: "POST",
				url: "http://profilestudio.net/memoryzoom.php",
				data: "zoom=" + (zoom+1),
				success: function(msg){
					
				},
				error: function(msg){
					alert(msg);
				}
			});
 
			
		} 
		
	});
	
	$('a.minus').click(function () {
		
		var galleryClass = $('#gallery div:first').attr('class');
		var zoom = Number(galleryClass.substring(4));	
		
		if (zoom > 1) {
			
			$("#gallery div").attr('class', 'gall' + (zoom - 1));
			$.ajax({
				type: "POST",
				url: "http://profilestudio.net/memoryzoom.php",
				data: "zoom=" + (zoom-1),
				success: function(msg){
					
				},
				error: function(msg){
					alert(msg);
				}
			});
		} 
		
	});
	
	
	function galresize () {
		
		var grid = 78;
		
		if ($('#gallery').width()) {
				
			if ($(window).width() > 630) {
				$('#gallery').width($(window).width() - 3*grid);
			} else {
				$('#gallery').width($(window).width());
			}
		
		}
		
		if ($('#grid').position()) {
			
			var position = $('#grid').position();
			var wwidth = $(window).width() - position.left;
			var wheight = $(window).height() - position.top;
			
			$('#grid').width(Math.floor(wwidth/grid)*grid);
			
			$('#grid').css("min-height",(Math.floor(wheight/grid)*grid));
			
		}
		
		if ($('#hometext').height()) {
				
			$('#homewrap').height() = $(window).height() + $('#hometext').height();
		
		}
				
	}
	galresize ();
	
	$(window).resize(function() {
		galresize ();
	});
	
	
	$(document).on({
		"contextmenu":function(e){e.preventDefault();},
		"dragstart":function(e){e.preventDefault();}
	});
	
	
	// Drag function /////////////////////////////////////////////////////////
	var ready = true,
			// Tells the app if the user is dragging the pointer
			dragging = false,
			// Stores the pointer starting X position for the pointer tracking
			pointerStartPosX = 0,
			// Stores the pointer ending X position for the pointer tracking
			pointerEndPosX = 0,
			// Stores the distance between the starting and ending pointer X position in each time period we are tracking the pointer
			pointerDistance = 0,
			// The starting time of the pointer tracking period
			monitorStartTime = 0,
			// The pointer tracking time duration
			monitorInt = 2000,
			// A setInterval instance used to call the rendering function
			ticker = 0,
			// Sets the speed of the image sliding animation
			speedMultiplier = 10,
			// CanvasLoader instance variable
			spinner,
			// Stores the total amount of images we have in the sequence
			totalFrames = 20,
			// The current frame value of the image slider animation
			currentFrame = 0,
			// Stores all the loaded image objects
			frames = [],
			// The value of the end frame which the currentFrame will be tweened to during the sliding animation
			endFrame = 0,
			// We keep track of the loaded images by increasing every time a new image is added to the image slider
			loadedImages = 0;

	function getPointerEvent(event) {
		return event.originalEvent.targetTouches ? event.originalEvent.targetTouches[0] : event;
	};

	// Touch event
	$("#fancybox-outer").live("touchstart", function (event) {

		// Prevents the original event handler behaciour
		//event.preventDefault();

		// Stores the pointer X position as the starting position
		pointerStartPosX = getPointerEvent(event).pageX;
		//window.alert(pointerStartPosX);
		// Tells the pointer tracking function that the user is actually dragging the pointer and it needs to track the pointer changes
		dragging = true;
	});

	$("#fancybox-outer").live("touchmove", function (event) {
		//window.alert('pointer');
		// Prevents the original event handler behaciour
		event.preventDefault();
		// Starts tracking the pointer X position changes
		trackPointer(event);

	});

	$("#fancybox-outer").live("touchend", function (event) {

		// Prevents the original event handler behaciour
		//window.alert(getPointerEvent(event).pageX);
		event.preventDefault();

		// Tells the pointer tracking function that the user finished dragging the pointer and it doesn't need to track the pointer changes anymore
		
		dragging = false;
		//window.alert("end");
	});
	/**

	* Tracks the pointer X position changes and calculates the "endFrame" for the image slider frame animation.

	* This function only runs if the application is ready and the user really is dragging the pointer; this way we can avoid unnecessary calculations and CPU usage.

	*/

	function trackPointer(event) {

			pointerEndPosX = getPointerEvent(event).pageX;

			if(monitorStartTime < new Date().getTime() - monitorInt) {

				// Calculates the distance between the pointer starting and ending position during the last tracking time period

				pointerEndPosX = pointerEndPosX - pointerStartPosX;

				if(pointerEndPosX >0 && (pointerEndPosX > 80 || pointerEndPosX < 80))
				{
					$.fancybox.prev();
					
				}else{
					
					$.fancybox.next();
				}

				monitorStartTime = new Date().getTime();

				// Stores the the pointer X position as the starting position (because we started a new tracking period)

				pointerStartPosX = getPointerEvent(event).pageX;

			}
	};
	
	
});

